package ltd.hxya.novel.crawl.config;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import io.swagger.models.auth.In;
import lombok.extern.slf4j.Slf4j;
import ltd.hxya.novel.common.constant.redis.RedisConstant;
import ltd.hxya.novel.common.framework.CustomFuture;
import ltd.hxya.novel.common.utils.BaseUtils;
import ltd.hxya.novel.common.utils.CrawlUtils;
import ltd.hxya.novel.common.utils.PatternUtils;
import ltd.hxya.novel.common.utils.RedisUtils;
import org.apache.avro.data.Json;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.Order;
import org.springframework.util.CollectionUtils;

import javax.annotation.PostConstruct;
import java.io.BufferedReader;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.regex.Matcher;

@Configuration
@Slf4j
public class IpConfig {

    @Autowired
    private RedisUtils redisUtils;
    @Autowired
    private ThreadPoolExecutor threadPoolExecutor;

    public void ipPool() throws IOException, InterruptedException {
        log.info("已经自动调用");
        CompletableFuture.runAsync(()->{

            for (int i = 1; i < 20; i++) {
                try {
                    Map<String,Integer> ipMap = crawlIp("https://www.kuaidaili.com/free/intr/"+i+"/");
                    Map<String, Integer> oldIpMap = redisUtils.get(RedisConstant.IP_POOL_KEY, new TypeReference<Map<String, Integer>>() {
                    });
                  //  Map<String,Integer> oldIpMap = new HashMap<>();
                    oldIpMap.forEach((key, value) -> {
                        ipMap.put(key, value);

                    });
                    //redisUtils.save(RedisConstant.IP_POOL_KEY, ipMap);
                    Thread.sleep(5000);
                } catch (IOException e) {
                    e.printStackTrace();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }

        },threadPoolExecutor);
        //checkIpIsAvailable();
        threadPoolExecutor.shutdown();
        threadPoolExecutor.awaitTermination(Integer.MAX_VALUE,TimeUnit.NANOSECONDS);
    }


    //验证是否可用
    public void checkIpIsAvailable() throws InterruptedException {
        Map<String, Integer> ipMap = redisUtils.get(RedisConstant.IP_POOL_KEY, new TypeReference<Map<String, Integer>>() {
        });
        if (CollectionUtils.isEmpty(ipMap)) {
            return;
        }
        //检验ip的可用性
        ScheduledThreadPoolExecutor executor = new ScheduledThreadPoolExecutor(10);
        executor.scheduleAtFixedRate(()->{
            ipMap.forEach((key, value) -> {
                String proxy = key + ":" + value;
                String url = "https://baidu.com";
                try {
                    CrawlUtils.execPython(url, proxy);
                } catch (Exception e) {
                    ipMap.remove(key);
                }
            });
            redisUtils.save(RedisConstant.IP_POOL_KEY, ipMap);
        },0,2,TimeUnit.MINUTES);



    }

    public Map<String, Integer> crawlIp(String url) throws IOException {
        Map<String, Integer> map = new HashMap<>();
        String body = CrawlUtils.entityToString(url, false);
        //log.info(body);
        Matcher matcher = PatternUtils.pattern("<td data-title=\"IP\">([\\s\\S]*?)</td>[\\s\\S]*?<td data-title=\"PORT\">([\\s\\S]*?)</td>", body);
        while (matcher.find()) {
            map.put(matcher.group(1), Integer.valueOf(matcher.group(2)));
            log.info("爬取到ip："+matcher.group(2));
            //urlList.add(url);
        }
        return map;
    }

}
